package com.cskaoyan.service.promotionService;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.AdExample;
import com.cskaoyan.bean.promotionVO.advertisementVO.AdListVO;
import com.cskaoyan.mapper.AdMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class AdServiceImpl implements AdService {

    @Autowired
    AdMapper adMapper;


    @Override
    public AdListVO adList(Integer page, Integer limit, String sort, String order,String name,String content) {
        PageHelper.startPage(page, limit);

        AdExample adExample = new AdExample();

        adExample.setOrderByClause(sort+" "+order);
        AdExample.Criteria criteria = adExample.createCriteria();
        criteria.andIdIsNotNull().andDeletedNotEqualTo(true);
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null && !"".equals(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        List<Ad> adList = adMapper.selectByExample(adExample);

        PageInfo<Ad> adPageInfo = new PageInfo<>(adList);
        long total = adPageInfo.getTotal();
        AdListVO adListVO = new AdListVO();
        adListVO.setItems(adList);
        adListVO.setTotal(total);
        return adListVO;
    }

    @Override
    public int adDelete(Ad ad) {

        int code = adMapper.updateByPrimaryKey(ad);
        return code;
    }

    @Override
    public Ad adUpdate(Ad ad) {

        ad.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        adMapper.updateByPrimaryKey(ad);
        ad=adMapper.selectByPrimaryKey(ad.getId());
        ad.setEndTime(null);
        ad.setStartTime(null);
        return ad;
    }
}
